package cn.io0288.ks.Adapter;

import static androidx.core.content.ContextCompat.getColor;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import cn.io0288.ks.ContactsBean;
import cn.io0288.ks.R;

/**
 * RecyclerView用联系人数据适配器
 */
public class ContactsBeanAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final Context mContext;
    private final ArrayList<ContactsBean> mCBList;

    @Override
    // 绑定复用视图容器对象到父视图
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ContactsBean mCB = mCBList.get(position);
        if (holder instanceof ContactsBeanViewHolder) {
            ContactsBeanViewHolder mHolder = ((ContactsBeanViewHolder) holder);
//            mHolder.iv.setImageResource(0);
            mHolder.tv_name.setText(mCB.name);
            if (mCB.tag.equals(ContactsBean.TAG_REMOTE)) {
                mHolder.v_tag.setBackgroundColor(getColor(mHolder.itemView.getContext(), R.color.teal_200));
            } else {
                mHolder.v_tag.setBackgroundColor(getColor(mHolder.itemView.getContext(), R.color.h_50));
            }
//            mHolder.tv_phone.setText(Arrays.toString(mCB.phone));
            mHolder.tv_phone.setText(String.join("; ", mCB.phone));
            if (mCB.phone.length > 1) mHolder.tv_phone.append("..More");
        }
    }

    public ContactsBeanAdapter(Context mContext, ArrayList<ContactsBean> mCBList) {
        this.mContext = mContext;
        this.mCBList = mCBList;
    }

    @NonNull
    @Override
    // 创建复用视图容器对象
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = null;
        RecyclerView.ViewHolder holder = null;
//        if (viewType > -1) {
            itemView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
            holder = new ContactsBeanViewHolder(itemView);
//        }
        if (holder == null){
            Log.i("holder", "holder is null");
        }
        return holder;
    }

    //复用视图容器类
    class ContactsBeanViewHolder extends RecyclerView.ViewHolder {
        // ImageView iv;
        View v_tag;
        TextView tv_name, tv_phone;

        public ContactsBeanViewHolder(@NonNull View itemView) {
            super(itemView);
//            iv = itemView.findViewById(R.id.item_iv);
            v_tag = itemView.findViewById(R.id.v_tag);
            tv_name = itemView.findViewById(R.id.item_name);
            tv_phone = itemView.findViewById(R.id.item_phone);
        }
    }

    @Override
    public int getItemCount() {
        if (mCBList == null)
            return 0;
        return mCBList.size();
    }
    public ContactsBean getItem(int position) {
        return mCBList.get(position);
    }

}


/**
 * ListView用联系人数据适配器
 */
//public class ContactsBeanAdapter extends ArrayAdapter<ContactsBean> {
//    //    public ArrayList<MyContacts> contacts = null;
//    static class ViewHolder {
//        ImageView iv;
//        TextView name;
//        TextView phone;
//
//        public ViewHolder(View v) {
//            name = v.findViewById(R.id.item_name);
//            phone = v.findViewById(R.id.item_phone);
//            // 关联
//            v.setTag(this);
//        }
//    }
//
//    public ContactsBeanAdapter(Context context, ArrayList<ContactsBean> contacts) {
//        super(context, 0, contacts);
//    }
//
//    public View getView(int position, View convertView, ViewGroup parent) {
//        ViewHolder holder;
//        // 获取 ViewHolder 实例
//        if (convertView == null) {
//            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
//            holder = new ViewHolder(convertView);
//        } else {
//            holder = (ViewHolder) convertView.getTag();
//        }
//        // 获取 ViewHolder 实例
//
//
//        // 使用 ViewHolder 更新视图数据
//        ContactsBean c = getItem(position);
//        if (c != null){
//            holder.name.setText("Name. "+c.name);
//            holder.phone.setText("Phone. "+c.phone[0].toString());
//            // 根据需要更新其他视图，例如图片等
//            if (c.phone.length > 1) holder.phone.append("…");
//        }
//        convertView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Log.i("lv", ""+view.getId());
//            }
//        });
//
//        return convertView;
//    }
//}
